package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.Banner;
import com.baizhi.service.BannerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    //展示
    @LogAnnotation(content = "查询轮播图",type = "select")
    @ResponseBody
    @RequestMapping("show")
    public Map showAll(int page, int limit){
        Map map = new HashMap();
        PageInfo<Banner> pageInfo = bannerService.showAll(page, limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }


    //添加
    @LogAnnotation(content = "添加轮播图",type = "add")
    @ResponseBody
    @RequestMapping("add")
    public Map add(Banner banner,String sw){
        Map map = new HashMap();
        try {
            if (sw.equals("on")){
                banner.setBannerState(1);
            }else {
                banner.setBannerState(0);
            }
            bannerService.add(banner);
            map.put("isAdd",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd",false);
        }
        return map;
    }

    //删除
    @LogAnnotation(content = "删除轮播图",type = "delete")
    @ResponseBody
    @RequestMapping("delete")
    public Map delete(int id){
        Map map = new HashMap();
        try {
            bannerService.updateState(id);
            map.put("isDelete",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete",false);
        }
        return map;
    }

    //查询
    @LogAnnotation(content = "查询单个轮播图",type = "selectOne")
    @ResponseBody
    @RequestMapping("selectOne")
    public Banner selectOne(int id){
        return  bannerService.showById(id);

    }

    //修改
    @LogAnnotation(content = "修改轮播图",type = "update")
    @ResponseBody
    @RequestMapping("update")
    public Map update(Banner banner,String sw){
        Map map = new HashMap();
        try {
            if (sw.equals("on")){
                banner.setBannerState(1);
            }else {
                banner.setBannerState(0);
            }
            bannerService.update(banner);
            map.put("isUpdate",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate",false);
        }
        return map;
    }


    //上传文件
    @ResponseBody
    @RequestMapping("upload")
    public Map upload(MultipartFile file) throws IOException {
        Map map = new HashMap();
        String s1 = UUID.randomUUID().toString() + file.getOriginalFilename();
        file.transferTo(new File("G:\\JAVA\\IDEAWorkspace\\cmfzdemo\\src\\main\\webapp\\img",s1));
        map.put("imageUrl","/img/"+s1);
        System.out.println("s1 = " + s1);
        return map;
    }

}
